welcome back now let's continue how to collect the data from different sources this time we're going to learn how to collect the data from Web or from the Internet and because that is the majority of the resources we can rely on every time we have a question just Google it and then we get a list of possible resources we can have the question answered and there are two ways of reading data from the web the first part of connecting data from web can be Wikipedia or wiki data those kind of data are open and you can have access to them any time and that covers a lot of things we may be interest in and for the wiki we can show it here and as long as the wiki contains certain table we can easily read them in because those tables are really structured and it will be quite simple and we can use the read HTML which is learned in read from a file part and then we can read in the table directly and this link is the path where we can get at HTML so rather than a local path a local data file we can use a web page as the source if we are calling read_html the file is already opened so basically this file is a list of countries by GDP and you may not be familiar with HTML but here has a clear table embedded in that file and this table will be the easiest part for our pandas to read from because the table structure is already predefined in panda's functionality so this tutorial is how to show you how we can retrieve this particular table just to by calling panda's functionality and then we can work on this table later on we first want to read the HTML from that table so that we get directly to this table area and in order to get the link you should go to the a link here and just copy that link right here so you get to the table part and then you can paste it here so that you can read everything you get from reading the HTML and the pass that to the table however it is not that straightforward because we are going to see what is actually inside of the table and we can see when we print everything inside of the table out we actually see a lot of dataframes these dataframes are sometimes they just show one line sometimes they just do multiple lines and there is just one dataframe is what we need this second for loop is simply to print out the name of the columns of that particular table so that when we print it out we can see that the first data frame is simply on an index and the second one is some other indexes and the 3rd one the multi index one looks like is the one we want because we have a country we have the different organization and we have different estimation we have different years so the 3rd one or index 2 indicates that is the data frame we want to achieve that is the reason in here I assigned index 2, the 3rd one and into our data frame and then we print it out in this one you can see that we have the indexes as numbers and we have the name of the columns and we have the values and they need a little bit more massage for sure but at least now we retrieve the table successfully we can print the info out so that we can have a better idea of the structure and we can see that each of the column corresponding to some of the information and the different organizations and year etc that indicates the table is successfully loaded okay so now we just learned how to get data from wiki and how to read data from predefined tables let's continue with the other part that is web scrapping because most of the time when you read a website the page is not wiki and you have to know some structure of that page in order to retrieve information from that page and in order to do the web scraping you are going to have extra libraries the first one is requests that will be a package you need to communicate with the server to get information from that page the other one is beautiful soup for you to understand the downloaded web page and to get the elements from them we are going to learn and we are going to practice them in this module okay so for the requests as I said request basically is to communicate with the server and get the package of all information on that page and you can first of all import requests and then you can use the get function for that particular file and on the web which is an HTML file and if your get is successful you will get the page as a response 200 basically that is the status code for successfully communicated with the server and it's like you say hi and you hear another hi from the other end and once we have that out you can see that the entire page is actually printed out like this there are just some code we calll it source code of the page and sometimes they are in HTML sometimes they are in an XML depends on the page and so that will be the thing you communicate with the server and then you get the package delivered back to you definitely we cannot directly work on this kind of information we have to digest it and then we can parse it to a certain way we can easily get information out so that will be the beautiful soup the beautiful soup is you can import from beautiful soup 4 and import this package and then you are going to call the beautiful soup and pass that page content remember we have the page status code and the page content all as the result of the communication of the get so you are going to put the page content to beautiful soup and then you are going to specify this is an HTML so we are going to use HTML parser to digest this page once you have that executed then you are going to see a little bit better outline of the page such as the document type such as the HTML structure of your page such as we have the head we have the body and we have the title and that is actually how the HTML is built and we as a human when we visit that page those kind of metadata is actually hidden they are just for machines to digest the information then if you have the beautifulsoup and soup.prettify() print it out and you can go ahead and find the tags Finding tags you can use the find_all() or find() is up to you find all will give you the other result and the find will give you one by one if you are going to find the title you can see that in the structure we have a little bit of title metadata wrapping "a simple example page" you can find out this is the element you are going to find and you are going to look for then once you have these elements you can print only the text out which means that you are going to get rid of this metadata and you are going to only print out the text file a simple example page now you get a sense of how we can retrieve certain information from the page by using requests to get the package and by using beautifulsoup to get the structure and using the find and find_all to get the particular tags or particular labels we are looking for if we are just looking for the text rather than the title we can search for the paragraph or the P if we search for the P we can see that in this example we have only one paragraph that is this is some simple content for this page once we have it there then we can get the text out and again you can use the find to print out them one by one as well and the find_all will basically give you all the results all up to you and we can again have a little bit sophisticated and this one we can see that is another HTML and this HTML we are just using this one as another practice we use the request to get the package of all the data on that HTML and the pass that to the page and then we pass the page content to beautifulsoup and the parse it Call the parser as HTML then we get a little bit harder and complex with this page which we have the table and we have some classes and we have some ID and we have other classes we have other ID basically this will be a information has some structure but we have to go and dig in so we learned the soup and find_all can find all the tags we need such as paragraphs and also the find all can help us to find out the classes as well because we see that this is a class of outer-text then we can search for that class name for the outer text map here so that each of the outer texts class will be listed as the result once we have the class printed out we can either go into the printed text file or we can do something else and we can also search about the element ID because now we can see we just want to find out the first item on the page and we can call the find all and search for the ID first then we immediately get to this part and the advantage of the soup is that you can have all those steps built in as a function so that you will search all the page as a similar structure and then you can scrap multiple page at a time and that will be really efficient so that is how we can read from the web and one easy way is read from wiki the harder way as read from any web page and you need to have the request and the beautiful soup and also a little bit HTML and the web page experience